/****************************************************************************
 * Copyright (c) 2022-2025 Imixs Software Solutions GmbH and others.
 * https://www.imixs.com
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 *
 * This Source Code may also be made available under the terms of the
 * GNU General Public License, version 2 or later (GPL-2.0-or-later),
 * which is available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
 ****************************************************************************/

package org.imixs.workflow.engine.plugins;

import java.util.List;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.exceptions.PluginException;

/**
 * This Pluginmodul cuts the length of the technical log entries generated by
 * the WorkflowKernel:
 * 
 * txtWorkflowPluginLog
 * 
 * txtWorkflowActivityLog
 * 
 * The Attribute numWorkflowLogLength indicates the maximum number of entries.
 * if <= 0 no limit is set.
 * 
 * 
 * @author Ralph Soika
 * @version 1.2
 * @see org.imixs.workflow.WorkflowManager
 * 
 */

public class LogPlugin extends AbstractPlugin {

    /**
     * the log entries generated form the kernel will be cut if the attribute
     * numWorkflowLogLength was provided
     */
    public ItemCollection run(ItemCollection documentContext, ItemCollection adocumentActivity) throws PluginException {
        List<?> vActivityLog;

        vActivityLog = documentContext.getItemValue("txtWorkflowActivityLog");

        // check if maximum length is defined
        int iMaxLogLength = documentContext.getItemValueInteger("numWorkflowLogLength");
        if (iMaxLogLength > 0) {

            while (vActivityLog.size() >= iMaxLogLength) {
                vActivityLog.remove(0);
            }

            // update Log entries now...
            documentContext.replaceItemValue("txtWorkflowActivityLog", vActivityLog);
        }

        return documentContext;
    }

}
